#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setCentralWidget(ui->splitter);

    ui->listWidget->setEnabled(false);

    // 按钮绑定action
//    ui->toolButton->setDefaultAction(ui->action);
//    ui->toolButton_2->setDefaultAction(ui->action2);
//    ui->toolButton_3->setDefaultAction(ui->action3);
//    ui->toolButton_4->setDefaultAction(ui->action4);
//    ui->toolButton_5->setDefaultAction(ui->action5);



}

MainWindow::~MainWindow()
{
    delete ui;
}

// 初始化列表
void MainWindow::on_action_triggered()
{
    QStringList slist;
    slist.append("hello");
    slist.append("world");
    ui->listWidget->addItems(slist);
}

// 清除列表
void MainWindow::on_action2_triggered()
{
    ui->listWidget->clear();
}

// 插入项
void MainWindow::on_action3_triggered()
{
    QListWidgetItem *item = new QListWidgetItem(QIcon(":/images/images/add"),"aaaa");
    item->setCheckState(Qt::Checked);
    ui->listWidget->insertItem(0,item);
}

/**
 * @brief MainWindow::on_checkBox_toggled
 * @param checked
 * 设置listwidget是否可以编辑
 */
void MainWindow::on_checkBox_toggled(bool checked)
{
    ui->listWidget->setEnabled(checked);
}

/**
 * @brief MainWindow::on_action4_triggered
 * 添加项
 */
void MainWindow::on_action4_triggered()
{
    QString str = ui->lineEdit->text();
    if(str.length()<1)
    {
        QMessageBox::information(this,"title","info:输入内容");
    }
    else
    {
        ui->listWidget->addItem(str);
    }
}
/**
 * @brief MainWindow::on_action5_triggered
 * 删除当前项
 */
void MainWindow::on_action5_triggered()
{
    int rowIndex = ui->listWidget->currentRow();
    ui->listWidget->takeItem(rowIndex);
}


/**
 * @brief MainWindow::on_listWidget_itemActivated
 * @param item
 * 设置选中的项为当前的项
 */
void MainWindow::on_listWidget_itemActivated(QListWidgetItem *item)
{
    ui->listWidget->setCurrentItem(item);
}
